Scripting FAQ
This list of scripting frequently asked questions (and answers) covers some common questions that are not easily categorized elsewhere. How to apply a visual effect? Visual effects are applied through the same mechanic as any other effect, namely the commands ApplyEffectAtLocation() or ApplyEffectToObject(). Whether the effect should be applied to an object or a location depends on a number of factors, but most of the time a little thinking about why the effect is appearing will give an answer. (For example, most traps apply a visual effect to the creature that triggered them. Spike traps, however, apply the spike visual at that creature's location since the spikes are not supposed to move when the creature does.) For the other times, a little trial-and-error may be necessary. Determining if a visual effect should be applied instantly or temporarily/permanently can require some trial-and-error as well. As a guideline, effects with a symbolic constant beginning with VFX_COM_, VFX_FNF_, or VFX_IMP_ should be applied instantly, while the prefixes VFX_BEAM_, VFX_DUR_, and VFX_EYES_ suggest being applied with a duration (or permanently). How to combine multiple scripts in one event? One approach to combining scripts is to simply copy one into the other. However, this often results in a script that will not compile for several reasons. An alternate approach is to create a "meta-script" that invokes each desired script via ExecuteScript(). For example, if the scripts heartbeat_01, heartbeat_02, and heartbeat_03 are all supposed to run in the same event, the following script could be used as the actual event handler. void main() { ExecuteScript("heartbeat_01", OBJECT_SELF); ExecuteScript("heartbeat_02", OBJECT_SELF); ExecuteScript("heartbeat_03", OBJECT_SELF); } This causes each of the three scripts to be executed in turn, and it avoids the problems involved with trying to copy one script into another. How to specify which immobile animations a creature uses? To specify particular animations for a creature, that creature needs its own script, rather than relying on BioWare's generic animation routines. The script would likely be for the OnHeartbeat event and utilize the ActionPlayAnimation() command. Multiple animations could be specified, but if the animations do not complete before the next OnHeartbeat event (six seconds), lag will be accumulated. In this context, the looping animations (ANIMATION_LOOPING_*) may be easier to deal with since they allow (require, in fact) specifying how long the animation should last. Alternatively, the fire-and-forget animations (ANIMATION_FIREFORGET_*) could be used if some possible delay with no animations at the end of each heartbeat is acceptable. How to tell if items belong to this module? Checking the ResRef with GetResRef() is probably the easiest way, if not the best way to do this. If all items in a given module have a certain code in their ResRef (such as having all ResRefs begin with a certain prefix), then any item whose ResRef does not begin with that prefix is not a custom item for that module. (Standard items' ResRefs begin with "nw_", "x0_", "x1_", or "x2_".) It is also possible to have the code vary based upon some determined criteria, such as a different code for each base item type. However, there is no real fool-proof way of doing this, particularly if the intent is to find items custom-made by a player (instead of items merely acquired in a previous module). So local vault servers (where this question usually comes up) probably should consider a different approach to whatever problem they are trying to solve. How to use the SoU projectile traps? The basic setup for projectile traps (introduced with Shadows of Undrentide) is to have a trigger defining what region is trapped and a placeable that serves as the source of the projectile. The configuration consists of two points: the trigger and placeable must have the same tag, and one of the x0_trap* scripts should be the OnTrapTriggered event handler for the trigger. A quick way to get started is the following. # In the trigger palette, locate and select the desired type of projectile trap. # Draw the trigger wherever desired. # In the placeable palette, locate a "projectile trap origin" of the same type of trap. # Place this wherever desired in the same area as the trigger. # The properties of both trigger and placeable can be changed, but they must share the same tag. In particular, if multiple projectile traps are to be in the same area, it may be necessary to change the tags so the game does not get confused. (Each trigger/placeable pair must have the same tag, but otherwise the tags are not restricted.) While it is not necessary to use a "projectile trap origin" placeable (any placeable will do), they are convenient in that they already have the correct tag for the corresponding trigger. External links * Newbe FAQ #2 @ BioWare's forum Category:NWScript